package com.jieyang.oa.controller.duty;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.entity.Duty;
import com.jieyang.oa.pojo.entity.Employee;
import com.jieyang.oa.pojo.Vo.DutyVo;
import com.jieyang.oa.service.DutyService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;

/**
 * @author 29276
 * @version 1.0
 * @description: 考勤管理
 * @date 2021/8/20 19:58
 */
@WebServlet("/dutyServlet")
public class DutyServlet extends BaseServlet {

    @Autowired
    private DutyService dutyService;

    /**
     * @Author 2927624517@qq.com
     * @Description 签到
     * @Date 2021/8/28 14:57
     * @Param [req, resp]
     * @return void
    **/
    public void signIn(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        resp.setContentType("text/html;charset=utf-8");

        Employee employee = (Employee) req.getSession().getAttribute("emp");
        String empId = employee.getEmpId();

        Integer integer = dutyService.signIn(empId);

        resp.getWriter().write(integer+"");

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 签退
     * @Date 2021/8/28 14:58
     * @Param [req, resp]
     * @return void
    **/
    public void signOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        Employee employee = (Employee) req.getSession().getAttribute("emp");
        String empId = employee.getEmpId();

        Integer integer = dutyService.signOut(empId);

        resp.getWriter().write(integer+"");
    }

    /**
     * @Author 2927624517@qq.com
     * @Description 查询签到签退记录
     * @Date 2021/8/28 14:58
     * @Param [req, resp]
     * @return void
    **/
    public void dutyList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        String empId = req.getParameter("empId");
        Integer deptNo;
        String no = req.getParameter("deptNo");
        if (no != null) {
            if ("".equals(no)) {
                deptNo = null;
            }
            else {
                deptNo = Integer.parseInt(no);
            }

        }
        else {
            deptNo = null;
        }
        Date dutyDate;
        String date = req.getParameter("dutyDate");
        if (date != null) {
            if ("".equals(date)) {
                dutyDate = null;
            }
            else {
                dutyDate = Date.valueOf(date);
            }

        }
        else {
            dutyDate = null;
        }
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        DutyVo dutyVo = new DutyVo();
        dutyVo.setEmpId(empId);
        dutyVo.setDeptNo(deptNo);
        dutyVo.setDutyDate(dutyDate);

        PageInfo<DutyVo> dutyVoPageInfo = dutyService.selectAll(pageNum, pageSize, dutyVo);

        resp.getWriter().write(new Gson().toJson(dutyVoPageInfo));

    }

    /**
     * @Author 2927624517@qq.com
     * @Description 我的签到记录
     * @Date 2021/8/28 14:58
     * @Param [req, resp]
     * @return void
    **/
    public void myDutyList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        resp.setContentType("text/html;charset=utf-8");

        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();
        int pageNum = Integer.parseInt(req.getParameter("pageNum"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        PageInfo<Duty> dutyPageInfo = dutyService.selectByEmpId(empId, pageNum, pageSize);
        resp.getWriter().write(new Gson().toJson(dutyPageInfo));

    }
}
